/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.cip.esymons.tramite.bc;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.cip.esymons.tramite.be.EnumTipoTramite;
import org.cip.esymons.tramite.be.Tramite;

/**
 *
 * @author Freddy Infantes
 */
@Stateless
public class TramiteFacade implements TramiteFacadeLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Tramite tramite) {
        em.persist(tramite);
    }

    public void edit(Tramite tramite) {
        em.merge(tramite);
    }

    public void remove(Tramite tramite) {
        em.remove(em.merge(tramite));
    }

    public Tramite find(Object id) {
        return em.find(Tramite.class, id);
    }

    public Tramite buscarXNombre(String nombre) {
        return (Tramite) em.createQuery("select object(o) from Tramite as o where o.trmNombre=?1").setParameter(1, nombre).getSingleResult();
    }

    public List<Tramite> findAll() {
        return em.createQuery("select object(o) from Tramite as o").getResultList();
    }

    public List<Tramite> listarXTipo(EnumTipoTramite tipo) {
        return em.createQuery("select object(o) from Tramite as o where o.tmrTipo= ?1").setParameter(1, tipo).getResultList();
    }

    public Tramite buscar(String desc) {
        try{
            Query q = null;
        StringBuilder sb = new StringBuilder();
        sb.append("select object(o) from Tramite as o Where o.trmNombre = ?1");

        q = em.createQuery(sb.toString());

        q.setParameter(1, desc);

        return (Tramite) q.getSingleResult();
        }catch(Exception e){
            return null;
        }
    }

    public List<Tramite> buscarPorNombre(String desc) {
        try{
            Query q = null;
        StringBuilder sb = new StringBuilder();
        sb.append("select object(o) from Tramite as o Where o.trmNombre like ?1");

        q = em.createQuery(sb.toString());

        q.setParameter(1, "%" + desc + "%");

        return q.getResultList();

        }catch(Exception e){
            return new ArrayList<Tramite>();
        }        
    }

    public List<Tramite> paginadorTramite(int cantidad, int posicion,String nom) {
        try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select t From Tramite t ");

            if (nom != null && !nom.isEmpty()) {
                sb.append(" Where t.trmNombre like ?1");
            }
            q = em.createQuery(sb.toString());

            if (nom != null && !nom.isEmpty()) {
                q.setParameter(1, "%" + nom + "%");
            }
            q.setFirstResult(posicion);
            q.setMaxResults(cantidad);
            return q.getResultList();
        } catch (Exception ex) {
            return new ArrayList();
        }
    }

    public int contarPaginadorTramite( String nom) {
       try {
            Query q = null;
            StringBuilder sb = new StringBuilder();
            sb.append("Select COUNT(a) From Tramite t ");

            if (nom != null && !nom.isEmpty()) {
                sb.append(" Where t.trmNombre =?1");
            }
            q = em.createQuery(sb.toString());

            if (nom != null && !nom.isEmpty()) {
                q.setParameter(1, nom);
            }
            return new Integer(q.getSingleResult().toString());
        } catch (Exception ex) {
            return 0;
        }
    }
}
